package com.cmic.rcs.emoji.util;

import org.springframework.stereotype.Component;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.Random;

/**
 * 验证码工具类
 */
@Component
public class CaptchaUtils {
    
    // 验证码宽度
    private static final int WIDTH = 120;
    // 验证码高度
    private static final int HEIGHT = 40;
    // 验证码字符个数
    private static final int CODE_COUNT = 4;
    // 干扰线数量
    private static final int LINE_COUNT = 10;
    // 验证码字符集
    private static final String CODE_CHAR = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz23456789";
    
    private static final Random random = new Random();
    
    /**
     * 生成随机验证码
     * @return 验证码字符串
     */
    public static String generateCode() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < CODE_COUNT; i++) {
            sb.append(CODE_CHAR.charAt(random.nextInt(CODE_CHAR.length())));
        }
        return sb.toString();
    }
    
    /**
     * 创建验证码图片
     * @param code 验证码字符串
     * @return Base64编码的图片
     */
    public static String createCaptchaImage(String code) {
        BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
        Graphics g = image.getGraphics();

        // 设置背景色
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, WIDTH, HEIGHT);

        // 设置边框
        g.setColor(Color.GRAY);
        g.drawRect(0, 0, WIDTH - 1, HEIGHT - 1);

        // 绘制干扰线
        for (int i = 0; i < LINE_COUNT; i++) {
            g.setColor(getRandomColor());
            int x1 = random.nextInt(WIDTH);
            int y1 = random.nextInt(HEIGHT);
            int x2 = random.nextInt(WIDTH);
            int y2 = random.nextInt(HEIGHT);
            g.drawLine(x1, y1, x2, y2);
        }

        // 绘制验证码文字
        g.setFont(new Font("Arial", Font.BOLD, 24));
        for (int i = 0; i < code.length(); i++) {
            g.setColor(getRandomColor());
            g.drawString(String.valueOf(code.charAt(i)), 25 + i * 20, 28);
        }

        // 将图片转换为Base64
        try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
            ImageIO.write(image, "png", outputStream);
            byte[] imageBytes = outputStream.toByteArray();
            String base64 = Base64.getEncoder().encodeToString(imageBytes);
            return "data:image/png;base64," + base64;
        } catch (IOException e) {
            throw new RuntimeException("生成验证码图片失败", e);
        } finally {
            g.dispose();
        }
    }
    
    /**
     * 获取随机颜色
     * @return 随机颜色
     */
    private static Color getRandomColor() {
        int r = random.nextInt(150);
        int g = random.nextInt(150);
        int b = random.nextInt(150);
        return new Color(r, g, b);
    }
    
    /**
     * 验证验证码是否正确
     * @param inputCode 用户输入的验证码
     * @param storedCode 存储的验证码
     * @return 是否正确
     */
    public static boolean validateCode(String inputCode, String storedCode) {
        if (inputCode == null || storedCode == null) {
            return false;
        }
        return inputCode.equalsIgnoreCase(storedCode);
    }
}